package com.zbest.zpay.paycore.dal.dao;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import com.zbest.common.lang.enums.BizCode;
import com.zbest.common.lang.exception.ZpayException;
import com.zbest.zpay.paycore.dal.mapper.PaySubInfoMapper;
import com.zbest.zpay.paycore.dal.model.PaySubInfoDO;

/**
 * Created by wenfeihu on 16/5/20.
 */
@Service("paySubInfoDAO")
public class PaySubInfoDAO {

    @Resource
    private PaySubInfoMapper paySubInfoMapper;

    public void insert(PaySubInfoDO paySubInfoDO) {

        try {
            paySubInfoMapper.insert(paySubInfoDO);
        } catch (DataIntegrityViolationException e) {
            throw new ZpayException(BizCode.DUPLICATED_ERROR);
        }

    }

    public void insertBatch(List<PaySubInfoDO> paySubInfoDOList) {

        try {
            paySubInfoMapper.insertBatch(paySubInfoDOList);
        } catch (DataIntegrityViolationException e) {
            throw new ZpayException(BizCode.DUPLICATED_ERROR);
        }

    }

    public void updateStatusAndInstSerialNo(String paySubInfoId, String instSerialNo,
                                            String destStatus, String srcStatus) {

        int row = paySubInfoMapper.updateStatusAndInstSerialNo(paySubInfoId, instSerialNo,
            destStatus, srcStatus, new Date());
        if (row <= 0) {
            throw new ZpayException(BizCode.DATA_NOT_EXISTS);
        }
    }

    public void updateStatus(String paySubInfoId, String destStatus, String srcStatus) {

        int row = paySubInfoMapper.updateStatus(paySubInfoId, destStatus, srcStatus, new Date());
        if (row <= 0) {
            throw new ZpayException(BizCode.DATA_NOT_EXISTS);
        }

    }

    public PaySubInfoDO query(String paySubInfoId) {
        return paySubInfoMapper.query(paySubInfoId);
    }

    public List<PaySubInfoDO> queryList(String payInfoId) {
        return paySubInfoMapper.queryList(payInfoId);
    }

}
